'use strict';

const fs = require('fs-extra')
const path = require('path')
const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx, app } = this;
    await ctx.render('pc/v2.html', {
      baseDir: app.baseDir.replace(/\\/g, '/'),
      love: 'you',
      blocksScripts: fs.readdirSync(path.join(app.baseDir, '/app/public/blocks')).map(fileBaseName => {
        return '/public/blocks/' + fileBaseName
      }),
      eleScripts: fs.readdirSync(path.join(app.baseDir, '/app/public/elements')).map(fileBaseName => {
        return '/public/elements/' + fileBaseName
      }),
      tplScripts: fs.readdirSync(path.join(app.baseDir, '/browser')).map(fileBaseName => {
        return  fileBaseName
      }),
    });
  }
  async tmp() {
    const { ctx, app } = this;
    let fileName = 'tmp/index.html'
    let filePath = path.join(app.baseDir, '/app/view/' + fileName)
    fs.ensureFileSync(filePath)
    await ctx.render(fileName, {
    })
  }
  async mobile() {
    const { ctx, app } = this;
    let fileName = 'm/index.html'
    let filePath = path.join(app.baseDir, '/app/view/' + fileName)
    fs.ensureFileSync(filePath)
    await ctx.render(fileName, {

    })
  }

  /**
   * @deprecated
   * @returns {Promise<void>}
   */
  async getfile() {
    const { ctx, app } = this;
    // ctx.downloader(path.join(app.baseDir, '/files/test3.vue'), 'test3.vue')

    let filename = 'test3.vue'
    ctx.status = 200;
    ctx.body = fs.readFileSync(path.join(app.baseDir, '/files/' + filename));
    ctx.set('content-type', this.app.config.downloader.content_type);
    ctx.attachment(filename);
    // console.log( path.join(app.baseDir, '/files/'+ filename))
    // ctx.body = fs.createReadStream(path.join(app.baseDir, '/files/' + filename));
  }
}

module.exports = HomeController;
